# from PIL import Image
import numpy as np
import cv2
import os
import time

img = cv2.imread("pp4.jpg")
lower_yellow = np.array([0, 170, 230])  # 设定黄色的阈值下限
upper_yellow = np.array([120, 255, 255])  # 设定黄色的阈值上限
kernel = np.ones((5, 5), np.uint8)

def TakePhoto():
    """

    :return: 拍的一帧照片
    """

    cap = cv2.VideoCapture(0)
    output_dir = ''
    time.sleep(1)
    ret, frame = cap.read()
    output_path = os.path.join(output_dir, "res.jpg")
    cv2.imwrite(output_path, frame)
    return frame


def Detect(img):

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 转换为灰色通道
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  # 转换为HSV空间
#    mask = cv2.inRange(hsv, lower_yellow, lower_yellow)
#     opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)  # 形态学开运算

    # bila = cv2.bilateralFilter(mask, 10, 200, 200)  # 双边滤波消除噪声
    # edges = cv2.Canny(mask, 50, 100)  # 边缘识别

    opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)  # 形态学开运算
    bila = cv2.bilateralFilter(img, 10, 200, 200)  # 双边滤波消除噪声
    edges = cv2.Canny(img, 50, 100)  # 边缘识别

    circles = cv2.HoughCircles(
        edges, cv2.HOUGH_GRADIENT, 1, 100, param1=100, param2=10, minRadius=10, maxRadius=500)
    cv2.imshow('edges', edges)
    cv2.waitKey(0)
    if circles is not None:
        return True
    else:
        return False


print(Detect(TakePhoto()))

